<template>
  <view class="search-box">
    <!-- 使用 uni-ui 提供的搜索组件 -->
    <!-- <uni-search-bar @input="input" :radius="100" cancelButton="none" focus="true"></uni-search-bar> -->
    <uni-search-bar @confirm="onconfirm" :radius="100" cancelButton="none" focus="true"></uni-search-bar>
    <!-- 搜索建议列表 -->
    <!--    <view class="sugg-list">
      <view class="sugg-item" v-for="(item, i) in searchResults" @click="gotoDetail(item.goods_id)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view> -->

    <!-- 搜索历史 -->
    <view class="history-box">
      <!-- 标题区域 -->
      <view class="search-item">搜索指定内容</view>
      <scroll-view scroll-x="true" style="white-space: nowrap;margin-top: 5px;">
        <view v-for="(item,i) in searchlist" :key="i" class="scroll-view-item" @click="onconfirm(item)">{{item}}</view>
      </scroll-view>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="20" @click="cleanhistory"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in history" :key="i" size="normal" type="error" :circle="true"
          class="mapa" @click="onconfirm(item)"></uni-tag>
      </view>
    </view>
    <ad unit-id="adunit-ca78ebba179b58fe" ad-type="video" ad-theme="white" style="position: fixed;bottom: 0;"></ad>
  </view>
</template>

<script>
  export default {
    computed: {
      history() {
        return [...this.historyList].reverse()
      }
    },
    data() {
      return {
        // timer: '', //延时器
        // kw: '', //关键字
        // searchResults: [],
        // 搜索关键词的历史记录
        historyList: [],
        kwvalue: '',
        searchlist: ["精选", "星空", "风景", "爱情", "二次元", "游戏", "插画", "科技", "创意", "汽车", "美食", "潮流", ]
      };
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kwvalue') || '[]')
    },
    methods: {
      input(e) {
        // clearTimeout(this.timer);
        // this.timer = setTimeout(() => {
        // this.kw = e
        // this.getSearchList()
        // }, 600)
      },
      async getSearchList() {
        // if(this.kw===''){
        //   this.searchResults=[]
        //   return
        // }
        // 发起请求，获取搜索建议列表
        // const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
        // if (res.meta.status !== 200) return uni.$showMsg()
        // this.searchResults = res.message
      },
      onconfirm(e) {
        if (e.value === '') return uni.$showMsg('禁止未填写内容进行搜索!', 3000)
        this.kwvalue = e.value || e
        uni.navigateTo({
          url: '/subpkg/searchlist/searchlist?confirm=' + this.kwvalue
        });
        this.saveSearchHistory(this.kwvalue)
      },
      saveSearchHistory(i) {
        // this.historyList.push(i)
        // 改造
        const set = new Set(this.historyList)
        set.delete(this.kwvalue)
        set.add(this.kwvalue)
        this.historyList = Array.from(set);
        uni.setStorageSync('kwvalue', JSON.stringify(this.historyList))
      },
      cleanhistory() {
        this.historyList = []
        uni.setStorageSync('kwvalue', '[]')
      },
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  // 搜索建议
  .sugg-list {
    margin-top: 1rpx;
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }

  .history-box {
    padding: 0 15px;
    color: gray;

    .search-item {
      border-bottom: 1px solid #efefef;
      font-size: 14px;
      height: 35px;
      line-height: 35px;
    }

    .scroll-view-item {
      display: inline-block;
      margin: 0 5px;
      font-size: 13px;
    }

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      // .uni-tag.data-v-1516016e {
      //   border-radius: 12px;
      // }
      .mapa {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
